<?php
namespace Mob \ Model;

use Think \ Model;
use Think \ Hook;
/**我的模型
 * Class InfoModel
 */

require_once ('./Application/Mob/Common/function.php');

class MyModel extends Model {

     protected $tableName = 'member';


     /**
	 * myTasks  我的任务
	 * @param $Uid       用户ID
	 * @param $TaskType   1=我发布进行中的任务,1=我发布的已经完成的任务，3=我接受的正在进行中的任务，4=我接受的已经完成的任务,0=代表所有任务
	 * @return $TaskInfo  返回任务信息
	 * @author:wanghongyan
	 */

	public function myTasks($Uid='',$TaskType=1) {
       $Uid=$Uid?$Uid:get_uid();  //判断一下是否传递UID信息，如果没有的话，调用get_uid方法获取。

       switch($TaskType)    //判断任务类型
       {

        case 1:   //如果类型为我发布正在进行中的任务
        $where['releaseUserId']= array('EQ',$Uid);
        $where['taskStatus']=array('IN','0,1,3,5,6,7,10,11,12');
        break;

        case 2://如果类型为我发布已经完成的任务
        $where['releaseUserId']= array('EQ',$Uid);
        $where['taskStatus']=array('IN','2,4,8');
        break;

        case 3:  //如果类型为不是我发布的，只是我接受的任务，并任务状态处理进行中的
        $accept = D('Accept');  //实例化模型
        $result = $accept->MyAgreeAccept($Uid);  //取我受理表中已经选择我的任务
        $result =array_column($result,'taskSerialno');
        $where['taskSerialno']= array('IN',$result);
        $where['taskStatus']=array('IN','0,1,3,5,6,7,10,11,12');
        break;

        case 4:
        $accept = D('Accept');  //实例化模型
        $result = $accept->MyAgreeAccept($Uid);  //取我受理表中已经选择我的任务
        $result =array_column($result,'taskSerialno');
        $where['taskSerialno']= array('IN',$result);
        $where['taskStatus']=array('IN','2,4');
        break;

        case 0:
        break;
        default:

       }
     $Tasks = M('Task')->where($where)->select();



     return $Tasks;
	}

	public function actIssue($uid,$type=1)
	{

	switch($type)
	{
	 case 1:
	 $activity = D('Activity')->where(" sponsorUid = '%s' and status  in (1) ",$uid)->field('activityId,Title,status,completionTime,browseCount,releaseTime,paySerialno,businessId,unix_timestamp(createTime) as createTime,isPay,peopleNum,isPay,FORMAT(amount/100,2) as amount ')->order('releaseTime desc')->select();
    // print_r(M()->getLastSql());

	 break;
	 case 2:
	 break;

	}

	foreach($activity as &$v)
	{
	 $v['commentCount']= M('Comment')->where(array('businessId'=>$v['activityId'],'status'=>1))->count();
	 if($v['isPay']==1)
	 {
	 $v['redpackcount']=D('RedPackRecord')->getdetailConut($v['paySerialno']);

	 }

	}

	 return $activity;

	}
	public function actPlayers($uid)
	{

   $players= $this->gainRedPack($uid);
   foreach($players as &$v)
   {

   $v['activity']= M('Activity')->where(array('paySerialno'=>$v['paySerialno']))->field('Title,sponsorUid')->find();

   }

   return $players;


	}
	public function gainRedPack($uid)
	{
      $where = array('applyUid'=>$uid,'applyStatus'=>1);
      $record = M('payment_record')->where($where)->field('paySerialno,unix_timestamp(applyTime) as applyTime,applyUid,FORMAT(money/100,2) as money,applyStatus')->order('applyTime desc')->select();
    return $record;

	}
	public function Comment($uid)
	{
	$where = array('commentUid'=>$uid,'status'=>1);

	$comment = M('comment')->where($where)->select();
	return $comment;



	}




}